/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.service.impl;

import com.wteam.modules.system.service.DevelopPlanCreditTypeMapService;
import com.wteam.modules.system.domain.DevelopPlanCreditTypeMap;
import com.wteam.modules.system.domain.dto.DevelopPlanCreditTypeMapDTO;
import com.wteam.modules.system.domain.criteria.DevelopPlanCreditTypeMapQueryCriteria;
import com.wteam.modules.system.domain.mapper.DevelopPlanCreditTypeMapMapper;
import com.wteam.modules.system.repository.DevelopPlanCreditTypeMapRepository;
import com.wteam.exception.BadRequestException;
import com.wteam.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.*;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 培养计划学分类型映射 业务实现层.
 *
 * @author herry_wong
 * @since 2020-12-29
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "developPlanCreditTypeMap")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class DevelopPlanCreditTypeMapServiceImpl implements DevelopPlanCreditTypeMapService {

    private final DevelopPlanCreditTypeMapRepository developPlanCreditTypeMapRepository;

    private final DevelopPlanCreditTypeMapMapper developPlanCreditTypeMapMapper;

    private final RedisUtils redisUtils;

    @Override
    public Map<String,Object> queryAll(DevelopPlanCreditTypeMapQueryCriteria criteria, Pageable pageable){
        Page<DevelopPlanCreditTypeMap> page = developPlanCreditTypeMapRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(developPlanCreditTypeMapMapper::toDto));
    }

    @Override
    public List<DevelopPlanCreditTypeMapDTO> queryAll(DevelopPlanCreditTypeMapQueryCriteria criteria){
        return developPlanCreditTypeMapMapper.toDto(developPlanCreditTypeMapRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
//    @Cacheable(key = "'id:' + #p0")
    public DevelopPlanCreditTypeMapDTO findDTOById(Long id) {
        DevelopPlanCreditTypeMap developPlanCreditTypeMap = developPlanCreditTypeMapRepository.findById(id).orElse(null);
        ValidUtil.notNull(developPlanCreditTypeMap, DevelopPlanCreditTypeMap.ENTITY_NAME, "id", id);
        return developPlanCreditTypeMapMapper.toDto(developPlanCreditTypeMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DevelopPlanCreditTypeMapDTO create(DevelopPlanCreditTypeMap resources) {
        return developPlanCreditTypeMapMapper.toDto(developPlanCreditTypeMapRepository.save(resources));
    }

    @Override
    @CacheEvict(key = "'id:' + #p0.id")
    @Transactional(rollbackFor = Exception.class)
    public void update(DevelopPlanCreditTypeMap resources) {
        DevelopPlanCreditTypeMap developPlanCreditTypeMap = developPlanCreditTypeMapRepository.findById(resources.getId()).orElse(null);
        ValidUtil.notNull(developPlanCreditTypeMap, DevelopPlanCreditTypeMap.ENTITY_NAME, "id",resources.getId());

        developPlanCreditTypeMap.copy(resources);
        developPlanCreditTypeMapRepository.save(developPlanCreditTypeMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Set<Long> ids) {
        redisUtils.delByKeys("developPlanCreditTypeMap::id:", ids);
        developPlanCreditTypeMapRepository.logicDeleteInBatchById(ids);
    }

    @Override
    public void download(List<DevelopPlanCreditTypeMapDTO> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DevelopPlanCreditTypeMapDTO developPlanCreditTypeMap : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("学分类别id", developPlanCreditTypeMap.getTypeId());
            map.put("培养计划id", developPlanCreditTypeMap.getPlanId());
            map.put("学分", developPlanCreditTypeMap.getCredit());
            map.put("创建时间", developPlanCreditTypeMap.getCreatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}